package com.gk.panda.apis.patrol.commons;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import lombok.Data;
import lombok.Getter;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author dubin
 * @create 2022-11-04 10:22
 */
@Data
@Component
public class DateUtils {

    //

    /**
     * @param ms 毫秒值转换
     * @return 列如:   11:12:12
     */
    public String msToTime(Long ms) {
        //天
        long day = ms / (24 * 60 * 60 * 1000);
        //小时
        long hour = (ms / (60 * 60 * 1000) - day * 24);
        //分钟
        long minute = ((ms / (60 * 1000)) - day * 24 * 60 - hour * 60);
        //秒
        long s = (ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60);
        if (day >= 1) {
            hour += day * 24;
        }
        String str = "";
//        if(day != 0){
//            str += day + ":";
//        }
        if (hour != 0) {
            str += hour + ":";
        }
        if (minute != 0) {
            str += minute + ":";
        }
        if (s != 0) {
            str += s;
        }
        return str;
    }

    /**
     * @param ms 毫秒值转中文时间
     * @return 列如:   11:12:12
     */
    public String msToHor(Long ms) {
        //天
        long day = ms / (24 * 60 * 60 * 1000);
        //小时
        long hour = (ms / (60 * 60 * 1000) - day * 24);
        //分钟
        long minute = ((ms / (60 * 1000)) - day * 24 * 60 - hour * 60);
        //秒
        long s = (ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60);
        if (day >= 1) {
            hour += day * 24;
        }
        String str = "";
//        if(day != 0){
//            str += day + "天";
//        }
        if (hour != 0) {
            str += hour + "";
        }
//        if(minute != 0){
//            str += minute + "分";
//        }
//        if(s != 0){
//            str += s + "秒";
//        }
        return str;
    }

    /**
     * 毫秒值转小时保留两位小数
     *
     * @param ms
     * @return
     */
    public String msToHour(String ms) {
        BigDecimal round = NumberUtil.round(Double.parseDouble(ms) / 1000 / 3600, 2);
        return round + "";
    }

    /**
     * @param start 开始时间
     * @param end   结束时间
     * @param sign  中间符号  时间格式 2022-11-2 11:27
     * @return 例如：2022-11-2 11:27 ~ 2022-11-2 11:27
     */
    public String sub(Date start, Date end, String sign) {
        //设置时间段
        String startDate = DateUtil.format(start, "yyyy.MM.dd HH:mm");
        String endDate = DateUtil.format(end, "yyyy.MM.dd HH:mm");
        return startDate + sign + endDate;

    }

    public String msToTimeC(Long ms) {
        //天
        long day = ms / (24 * 60 * 60 * 1000);
        //小时
        long hour = (ms / (60 * 60 * 1000) - day * 24);
        //分钟
        long minute = ((ms / (60 * 1000)) - day * 24 * 60 - hour * 60);


        return day + "天" + hour + "小时" + minute + "分";
    }
    /*
     *保留2位小数 以小时为单位
     */
    public static String msHour(Long ms) {
        double hour =(double) ms / (1000 * 60 * 60);
         BigDecimal round = NumberUtil.round(hour, 2);
        return round + "";
    }
    /**
     *保留3位小数 以小时为单位
     */
    public static String msHour3(Long ms) {
        double hour =(double) ms / (1000 * 60 * 60);
        BigDecimal round = NumberUtil.round(hour, 3);
        return round + "";
    }



}
